package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Order;
import cn.hrlly.tao2shou.listener.OnOrderDetailsListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-31
 * Time: 18:42
 */

public class OrderDetailsCallback extends Callback<Order> {

    private OnOrderDetailsListener onOrderDetailsListener;

    public OrderDetailsCallback(OnOrderDetailsListener onOrderDetailsListener) {
        this.onOrderDetailsListener = onOrderDetailsListener;
    }

    @Override
    public Order parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        Order order=new Gson().fromJson(string,Order.class);
        return order;
    }

    @Override
    public void onError(Request request, Exception e) {
        onOrderDetailsListener.showOrderDetailsFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(Order order) {
        if(order.getStatus()==0)
            onOrderDetailsListener.showOrderDetailsSucc(order);
        else
            onOrderDetailsListener.showOrderDetailsFail(order.getErrorMessage());
    }
}
